package com.dragon.repository.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.dragon.repository.config.annotation.DataScope;
import com.dragon.repository.entity.Admin;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;

import java.util.List;

/**
 * <p>
 *  Mapper 接口
 * </p>
 *
 * @author sy
 * @since 2023-06-12
 */
@Mapper
public interface AdminMapper extends BaseMapper<Admin> {


    @Select("SELECT M.*,N.name AS department_name FROM " +
            "             (SELECT A.*,B.department_id FROM dragon_admin A " +
            "            JOIN dragon_admin_department B ON A.id = B.admin_id WHERE A.id=#{id})M,dragon_department N WHERE M.department_id = N.id")
    Admin selectInfoByAdminId(Integer id);

    @Select("<script>" +
            "SELECT M.*,N.department_id,V.name AS departmentName FROM " +
            " (SELECT a.*,b.role_id AS roleId,c.role_name AS roleName ,d.position_id, e.name AS positionName FROM dragon_admin a " +
            " LEFT JOIN dragon_admin_role b ON a.id = b.admin_id " +
            " LEFT JOIN dragon_role c ON b.role_id = c.id " +
            " LEFT JOIN dragon_admin_position d ON a.id= d.admin_id" +
            " LEFT JOIN dragon_position e on e.id = d.position_id "+
            " WHERE 1=1 and a.deleted = 0 and a.account_type = 2 " +
            "<if test='search!=null and search != \"\"'>" +
            "and (a.username like CONCAT('%',#{search},'%') or a.phone like CONCAT('%',#{search},'%') or a.account like CONCAT('%',#{search},'%') )" +
            "</if>"+
            ")M LEFT JOIN dragon_admin_department N ON M.id = N.admin_id " +
            "LEFT JOIN dragon_department V on V.id = N.department_id WHERE 1=1 " +
            "<if test='departmentId.size != 0 and departmentId != \"\"'> "+
            " AND N.department_id in " +
            "<foreach collection = 'departmentId' item = 'item' open = '(' close = ')' separator = ','>" +
            "#{item}" +
            "</foreach>" +
            "</if>" +
            "</script>")
    Page<Admin> selectPageBySearch(Page<Admin> adminPage, String search, List<Integer> departmentId);

    @Select(" select a.username from dragon_admin a where a.id = #{adminId}")
    String selectUsernameById(Integer adminId);

    @Select("select da.username,da.id from dragon_admin da " +
            "left join  dragon_admin_department dad on da.id = dad.admin_id where deleted = 0 and da.id !=#{adminId}")
    @DataScope(adminAlias = "da",adminField = "id",deptField = "department_id",deptAlias = "dad",authCodeString = "quoteManagement")
    List<Admin> selectAdminList(Integer adminId);

}
